package com.liziguo.lzgfp.server.dao;

import com.liziguo.lzgfp.server.entity.Forward;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Liziguo
 * @Date: 2021/12/26 15:40
 */
@Repository
public class ForwardDao {

    private static JdbcTemplate jdbcTemplate;

    public static List<Forward> selectByUserId(int userId) {
        return jdbcTemplate.query("select * from forward where user_id=?", new BeanPropertyRowMapper<>(Forward.class), userId);
    }

    public static Forward selectByPort(int port) {
        List<Forward> list = jdbcTemplate.query("select * from forward where serverport=?", new BeanPropertyRowMapper<>(Forward.class), port);
        if (!list.isEmpty()) return list.get(0);
        return null;
    }

    public static int deleteByUserId(int userId) {
        return jdbcTemplate.update("delete from forward where user_id=?", userId);
    }

    public static int insert(Forward forward) {
        return jdbcTemplate.update("insert into forward values(?,?,?,?)", forward.getServerport(), forward.getLocalhost(), forward.getLocalport(), forward.getUserId());
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate template) {
        jdbcTemplate = template;
    }
}
